﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace BabyShop.CP
{
    public partial class Brand : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGrid();
            }
            lblMsg.Text = "";
        }

        public void FillGrid()
        {
            DataTable dt = DAO.Brand.getAll();
            if (dt.Rows.Count > 0)
            {
                GridView.DataSource = dt;
                GridView.DataBind();
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                GridView.DataSource = dt;
                GridView.DataBind();

                int TotalColumns = GridView.Rows[0].Cells.Count;
                GridView.Rows[0].Cells.Clear();
                GridView.Rows[0].Cells.Add(new TableCell());
                GridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                GridView.Rows[0].Cells[0].Text = "Không có dữ liệu";
            }
        }

        protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView.PageIndex = e.NewPageIndex;
            GridView.DataBind();
        }

        protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView.EditIndex = -1;
            FillGrid();
        }

        protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                TextBox txtNewName = (TextBox)GridView.FooterRow.FindControl("txtNewName");
                string newName = txtNewName.Text.Trim();
                if (newName.Length > 0)
                {
                    DAO.Brand.Insert(newName);
                    FillGrid();
                }
                else
                {
                    lblMsg.Text = "Xin vui lòng nhập tên cần thêm.";
                }
            }
        }

        protected void GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = Convert.ToInt32(GridView.DataKeys[e.RowIndex].Values[0].ToString());            
            DAO.Brand.Delete(id);
            FillGrid();
            lblMsg.Text = "Đã xóa nhà cung cấp ID:" + id.ToString();
        }

        protected void GridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView.EditIndex = e.NewEditIndex;
            FillGrid();
        }

        protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            TextBox txtName = (TextBox)GridView.Rows[e.RowIndex].FindControl("txtName");
            Label lbID = (Label)GridView.Rows[e.RowIndex].FindControl("lbID");
            if (txtName.Text.Trim().Length > 0)
            {
                DAO.Brand.Update(Convert.ToInt32(lbID.Text), txtName.Text);
                GridView.EditIndex = -1;
                FillGrid();
            }
            else
            {
                lblMsg.Text = "Xin vui lòng nhập tên vào.";
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string name = txtKeyword.Text.Trim();
            DataTable dt = DAO.Brand.find(name);
            if (dt.Rows.Count > 0)
            {
                GridView.DataSource = dt;
                GridView.DataBind();
            }
            else
            {
                dt.Rows.Add(dt.NewRow());
                GridView.DataSource = dt;
                GridView.DataBind();

                int TotalColumns = GridView.Rows[0].Cells.Count;
                GridView.Rows[0].Cells.Clear();
                GridView.Rows[0].Cells.Add(new TableCell());
                GridView.Rows[0].Cells[0].ColumnSpan = TotalColumns;
                GridView.Rows[0].Cells[0].Text = "Không có dữ liệu";
            }
        }
    }
}
